<template>
	<view>
		<button v-if="is_style=='button'" class="flex j-c a-c b_y cff but_login" style="background-color: #111111;"
			@tap="on_login" data-eventsync="true" bindtap="getUserProfile" scope="userInfo" open-type="getUserInfo">
			抖音授权登录
		</button>

		<view v-if="is_style=='img'">
			<button @tap="on_login" data-eventsync="true" bindtap="getUserProfile" scope="userInfo"
				open-type="getUserInfo">
				<image class="login_icon" src="@/static/icon/douyin.png">
				</image>
			</button>
		</view>
	</view>
</template>

<script>
	export default {
		name: "login-mp-weixin",
		props: {
			userName: {
				type: String,
				default: ''
			},
			avatarUrl: {
				type: String,
				default: ''
			},
			agr: {
				type: Boolean,
				default: false
			},
			agr: {
				type: Boolean,
				default: false
			},
			is_style: {
				type: String,
				default: 'button'
			},
		},
		data() {
			return {}
		},
		created() {
			this.face = this.avatarUrl;
			this.nickname = this.userName;
		},

		methods: {
			on_login() {
				let that = this;
				if (!this.agr) {
					uni.showToast({
						title: '请勾选同意协议',
						icon: "none"
					});
					return;
				}
				uni.showLoading({
					title: '正在登录中...',
					mask: true
				})
				uni.getUserProfile({
					success(res) {
						console.log("res: " + JSON.stringify(res));
						that.nickname = res.userInfo.nickName;
						that.face = res.userInfo.avatarUrl;
						that.sex = res.userInfo.gender;

					 

						uni.login({
							provider: 'toutiao',
							success: function(loginRes) {
								let dou_code = loginRes.code;
								let dou_anycode = loginRes.anonymousCode;
								let requestParams = {
									invite_code: uni.getStorageSync('invite_code'),
									face: that.face,
									nickname: that.nickname,
									dou_code: dou_code,
									dou_anycode: dou_anycode,
									sex: that.sex,
									tel: '',
									checknum: '',
								};
								that.Banmikeji.douyin_login(requestParams).then(ret => {
									that.$emit('confirm', ret);
								})
							},
							fail(err) {
								console.log("调用login失败:" + JSON.stringify(err));
							}


						});



					},
					fail(err) {
						console.log("获取用户信息失败:" + JSON.stringify(err));

					}
				})


			},

		}
	}
</script>
<style lang="scss">
	button::after {
		border: none;
	}

	button {
		padding: 0;
		background-color: transparent;

	}
</style>